package com.wgj.android.zfp;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Config;
import android.util.Log;

import com.google.gson.Gson;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.entity.Message;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2017/12/1.
 */

public class MessageReciever extends BroadcastReceiver {
    private List<String> list = new ArrayList<String>();
    private NotificationManager nm;
    @Override
    public void onReceive(Context context, Intent intent) {

        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }

        Bundle bundle = intent.getExtras();
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String str="";
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {

            processCustomMessage(context, bundle);

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                String str="";
           // receivingNotification(context,bundle);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            String str="";
            //openNotification(context,bundle);

        }
    }
    private void processCustomMessage(Context context, Bundle bundle) {
        String title = bundle.getString(JPushInterface.EXTRA_TITLE);

        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        Log.i("tainyun","message" +message);
        parseJson(message);
        EventBus.getDefault().postSticky( new Gson().fromJson(message, Message.class));
        list.add(message);
       // NotificationHelper.showMessageNotification(context, nm, title, message, channel);
    }
    public  String parseJson(String str){

        String string="";
        try {
            JSONObject json = new JSONObject(str);
            string = json.getString("content");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return string;
    }


}
